package cn.lzd.ui.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import cn.lzd.ui.R;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;

/**
 * @author lizhidong
 */
public class GlideUtil {
    /**
     * 加载 drawable 到ImageView，并适配圆角
     * @param cornerSize 圆角尺寸 px
     */
    public static void loadDrawable2Img(ImageView imageView, int resId, int errorResId, int cornerSize) {
        RequestOptions options = RequestOptions.bitmapTransform(new RoundedCorners(cornerSize))
                .error(errorResId);
        Glide.with(imageView.getContext()).load(resId).apply(options).into(imageView);
    }

    /**
     * 加载url图片到ImageView
     */
    public static void loadUrl2Img(String url, ImageView imageView) {
        loadUrl2View(null, url, imageView);
    }

    /**
     * 加载url图片到ImageView
     */
    public static void loadUrl2View(Context context, String url, View view) {
        context = context == null ? ActivityUtils.getTopActivity() : context;
        if (!RegexUtils.isURL(url)) {
            return;
        }
//        GlideUrl glideUrl = new GlideUrl(url, Constant::getHeaderMap);
        Glide.with(context).load(url).error(R.drawable.uibase_circle_gray)
                .into(new CustomTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        if (resource instanceof GifDrawable) {
                            applyDrawable(resource);
                            ((GifDrawable) resource).start();
                        } else {
                            applyDrawable(resource);
                        }
                    }

                    private void applyDrawable(Drawable drawable) {
                        if (view instanceof ImageView) {
                            ((ImageView) view).setImageDrawable(drawable);
                        } else {
                            view.setBackground(drawable);
                        }
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });
    }

    /**
     * Glide 加载圆形图片
     */
    public static void loadCircle2Img(Context context, String url, ImageView imageView) {
        Glide.with(context).load(url).error(R.drawable.uibase_circle_gray)
                .apply(new RequestOptions().circleCrop()).into(imageView);
    }

    /**
     * Glide 加载圆形图片 到 backGround
     */
    public static void loadCircle2View(Context context, String url, View view) {
        Glide.with(context).load(url).error(R.drawable.uibase_circle_gray)
                .apply(new RequestOptions().circleCrop()).into(new CustomTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        view.setBackground(resource);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                    }
                });
    }
}